package net.a6polo.a6poloweibo;

import java.io.IOException;

import net.a6polo.a6poloweibo.sina.domain.Timeline;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView.OnRefreshListener;
import net.a6polo.a6poloweibo.view.MyProgressDialog;
import net.a6polo.service.Service;
import net.a6polo.service.SnsType;
import net.a6polo.service.domain.Account;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.StatusesAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private Handler handler = null;

	private Timeline timeline = null;

	private DropDownToRefreshListView timeLineListView = null;
	
	private SinaTimelineListViewAdapter sinaTimelineListViewAdapter;

	private Exception exception;
	
	private int pageNo = 1;
	
	private int pageSize = 20;
	
	private long maxId = 0;
	
	private Dialog progressDialog;
	
	private Service service;
	
	private Account sinaAccount;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		service = new Service(MainActivity.this);
		sinaAccount = service.getLastAccountByType(SnsType.SINA_WEIBO);
		
		progressDialog = new MyProgressDialog(this, R.style.CustomProgressDialog); 

		handler = new Handler();
		timeLineListView = (DropDownToRefreshListView) findViewById(R.id.timeLineListView);
		Button timelineButton = (Button) findViewById(R.id.btn_refresh);
		timelineButton.setOnClickListener(this);

		Button writeButton = (Button) findViewById(R.id.btn_writer);
		writeButton.setOnClickListener(this);

		TextView title = (TextView) findViewById(R.id.txt_wb_title);
		title.setText(sinaAccount.getUserName());

		timeLineListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				timelineButtonListener();
				timeLineListView.onRefreshComplete("松开可刷新");
			}

		});

		timeLineListView.setOnLoadMoreListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				more();
				timeLineListView.onRefreshComplete("正在加载");

			}

		});

		if (null == timeline || timeline.getStatuses().size() == 0) {
			progressDialog.show();
			timelineButtonListener();
			// refresh();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_refresh: {
			timelineButtonListener();
			break;
		}
		case R.id.btn_writer: {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, Writeweiboactivity.class);
			startActivity(intent);

			break;
		}
		default:
			break;
		}

	}

	public void timelineButtonListener() {
				
		progressDialog.show();
		
		new Thread() {
			public void run() {				
				Oauth2AccessToken accessToken = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());
				StatusesAPI api = new StatusesAPI(accessToken);
				api.friendsTimeline(maxId, (long) 0, pageSize, pageNo, false, WeiboAPI.FEATURE.ALL, false,
						new MyRequestListener());
				

			}
		}.start();

	}

	class MyRequestListener implements RequestListener {

		@Override
		public void onComplete(String arg0) {
			Gson gson = new Gson();
			Log.d("a6polo", "result:" + arg0);
			timeline = gson.fromJson(arg0, Timeline.class);
			Log.d("a6polo", "result size:" + timeline.getStatuses().size());
			handler.post(runnableUi);
			
			int maxIndex = timeline.getStatuses().size();
			
			if(maxIndex > 0) {
				maxIndex = maxIndex -1;
			}
			
			maxId = timeline.getStatuses().get(maxIndex).getId();
			
			progressDialog.dismiss();
		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});
		}

	}
	
	Runnable runnableUi = new Runnable() {
		@Override
		public void run() {
			loadDate(timeline);
			//timeLineListView.onRefreshComplete("更新完成");
		}
	};
	
	class MyMoreRequestListener implements RequestListener {

		@Override
		public void onComplete(String arg0) {
			Gson gson = new Gson();
			Log.d("a6polo", "result:" + arg0);
			timeline = gson.fromJson(arg0, Timeline.class);
			Log.d("a6polo", "result size:" + timeline.getStatuses().size());
			handler.post(runnableMoreUi);
			
			progressDialog.dismiss();
		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});
		}

	}
	
	Runnable runnableMoreUi = new Runnable() {
		@Override
		public void run() {
			addData(timeline);
			
		}
	};

	public void loadDate(Timeline timeline) {
		sinaTimelineListViewAdapter = new SinaTimelineListViewAdapter(timeline, this);
		timeLineListView.setAdapter(sinaTimelineListViewAdapter);

	}
	
	public void addData(Timeline timeline) {
		sinaTimelineListViewAdapter.addItem(timeline);		
		sinaTimelineListViewAdapter.notifyDataSetChanged();


	}

	public void more() {
		//pageNo++;
		new Thread() {
			public void run() {
				Oauth2AccessToken accessToken = new Oauth2AccessToken(sinaAccount.getAccessToken(), sinaAccount.getExpiresIn());
				StatusesAPI api = new StatusesAPI(accessToken);
				api.friendsTimeline((long) 0, maxId, pageSize, pageNo, false, WeiboAPI.FEATURE.ALL, false,
						new MyMoreRequestListener());
				
			}
		}.start();

	}

}
